<template>
  <div class="box">
    <img src="@/assets/centerLogo.webp" style="width: 100%; height: 52px" />
    <!--头部  -->
    <div class="header">
      <div class="h_left">购物车</div>
      <div class="h_right">
        <span class="r-s1">领劵</span>
        <span class="r-s2">编辑</span>
      </div>
    </div>
    <!-- 中间的部分 -->
    <div class="center">
      <!-- 3项服务 -->
      <div class="service" v-if="good.length > 0">
        <div class="service_item">30天无忧退货</div>
        <div class="service_item">48小时快速退款</div>
        <div class="service_item">满99元包邮</div>
      </div>
      <!-- 第三栏 -->
      <div class="discount" v-if="good.length > 0">
        <div class="dis_top">
          <div class="txt1">
            <span>全场换购</span>
            已满足1件享低至5折超值换购
          </div>
          <div class="txt2">
            再逛逛
            <van-icon name="arrow" />
          </div>
        </div>
        <div class="dis_bottom">
          <a href="https://m.you.163.com/item/newItem">
            <span>去换购商品</span>

            <van-icon name="arrow" />
          </a>
        </div>
      </div>
      <!-- 购买的商品 -->
      <div class="goods">
        <div class="good_item" v-for="(item, index) in good" :key="index">
          <div class="good_center">
            <i class="checkbox good_i" :class="{ checked: item.isChecked }" @click="changeChecked(item, $event)" ref="ii"></i>
            <div class="gd">
              <div class="gd_img">
                <img v-lazy="item.imgUrl" alt="" @click="goDetail(item.skuId)" />
              </div>
              <div class="gd_txt">
                <p class="gd_txt_p">
                  <span>超值捡漏 &nbsp;</span>
                  {{ item.skuName }}
                </p>
                <div class="type">静谧蓝，M(165/84A)&nbsp; ; &nbsp;菊 <van-icon name="arrow-down" /></div>
                <div class="price">¥{{ item.skuPrice }} <i>¥9999</i></div>
                <div class="gd_count">
                  <button class="gd_btn" @click="minusOne(item)">-</button>
                  <div class="gd_input">{{ item.skuNum }}</div>
                  <button class="gd_btn" @click="addOne(item)">+</button>
                </div>
              </div>
              <div class="gt_bottom"><van-icon name="wap-home" color="#75A6E5" size="10" />自营仓商品满99包邮</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 结算栏 -->
    <div class="checkstand">
      <i class="checkbox" :class="{ checked: totalLight.length == good.length }" @click="allChange($event)"></i>
      <p class="selected">已选({{ totalSelct }})</p>
      <p class="total">合计:¥{{ totalPrice }}</p>
      <div class="make_order" :class="{ ok: totalLight.length > 0 }" @click="goOrder($event)">下单</div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import throttle from "lodash/throttle";
export default {
  // 组件名字
  name: "shopcart",
  // 状态数据
  data() {
    return {};
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.$store.dispatch("shopcart/getcartlist");
    },
    addOne: throttle(async function (sku) {
      try {
        await this.$store.dispatch("shopcart/addOneFun", { skuId: sku.skuId, skuNum: 1 });
        this.getData();
      } catch (error) {
        alert("出错了");
      }
    }, 1000),
    minusOne: throttle(async function (sku) {
      try {
        if (sku.skuNum > 1) {
          await this.$store.dispatch("shopcart/minusOneFun", { skuId: sku.skuId, skuNum: -1 });
          this.getData();
        }
      } catch (error) {
        alert("出错了");
      }
    }, 1000),
    changeChecked: throttle(async function (sku, event) {
      try {
        let flag = "";
        let res = event.target.className;
        if (res.includes("checked")) {
          flag = 0;
        } else {
          flag = 1;
        }
        console.log(res);
        await this.$store.dispatch("shopcart/changeCheckedFun", { skuId: sku.skuId, isChecked: flag });
        this.getData();
      } catch (error) {
        alert("出错了");
      }
    }, 300),
    allChange: throttle(async function (event) {
      try {
        let isChecked = "";
        let res = event.target.className;
        if (res.includes("checked")) {
          isChecked = 0;
        } else {
          isChecked = 1;
        }
        await this.$store.dispatch("shopcart/allChangeFun", isChecked);
        this.getData();
      } catch (error) {
        alert("出错了");
      }
    }, 300),
    // 点击跳转订单的页面
    goOrder(event) {
      if (event.target.className.includes("ok")) {
        this.$router.push("/order");
      }
    },
    goDetail(skuId) {
      this.$router.push({ name: "detail", params: { skuId } });
    },
  },
  computed: {
    ...mapGetters("shopcart", ["good"]),
    totalPrice() {
      return this.good.reduce((prev, next) => {
        return prev + next.skuNum * next.skuPrice;
      }, 0);
    },
    totalSelct() {
      return this.good.reduce((one, two) => {
        return one + two.skuNum;
      }, 0);
    },
    totalLight() {
      return this.good.filter((item) => {
        return item.isChecked == 1;
      });
    },
  },
};
</script>
<style lang="less" scoped>
.box {
  color: #333;
  width: 100%;
  height: 667px;
  background-color: #eee;
  .header {
    height: 44px;
    width: 100%;
    padding: 0 15px;
    box-sizing: border-box;
    border-bottom: 1px solid #ccc;
    background-color: #fff;
    .h_left {
      float: left;
      font-size: 18px;
      line-height: 44px;
    }

    .h_right {
      float: right;
      height: 44px;
      .r-s1 {
        font-size: 12px;
        color: #fff;
        margin-right: 20px;
        line-height: 43px;
        width: 36px;
        height: 19px;
        background-image: linear-gradient(-45deg, #fc865f 0, #fc625f 100%);
        padding: 2px 6px;
        position: relative;
        z-index: 1;
        &::after {
          display: block;
          content: "";
          width: 6px;
          height: 8px;
          border-radius: 15px;
          background-color: white;
          position: absolute;
          right: -3px;
          top: 5px;
          z-index: 999;
        }
        &::before {
          display: block;
          content: "";
          width: 6px;
          height: 8px;
          border-radius: 15px;
          background-color: white;
          position: absolute;
          left: -3px;
          top: 5px;
          z-index: 999;
        }
      }
      .r-s2 {
        line-height: 43px;
        font-size: 15px;
      }
    }
  }
  .center {
    height: calc(100vh - 152px);
    overflow-y: scroll;
    .service {
      width: 100%;
      height: 35px;
      background-color: #eee;
      display: flex;
      font-size: 12px;
      justify-content: space-around;
      align-items: center;
      .service_item {
        color: #666;
        position: relative;
        &::before {
          display: block;
          content: "";
          width: 3px;
          height: 3px;
          border-radius: 50%;
          background-color: transparent;
          border: 1px solid #bbb;
          position: absolute;
          left: -10px;
          top: 5px;
        }
      }
    }
    .discount {
      width: 100%;
      height: 73px;
      background-color: #fff;
      .dis_top {
        width: 345px;
        height: 20px;
        line-height: 20px;
        padding: 10px 15px 10px 0px;
        margin-left: 15px;
        .txt1 {
          float: left;
          font-size: 14px;
          span {
            padding: 2px;
            font-size: 12px;
            margin-right: 5px;
            background-color: #fc865f;
            color: #fff;
            border-radius: 2px;
          }
        }
        .txt2 {
          float: right;
          font-size: 12px;
          color: #dd1a21;
          margin-left: 20px;
        }
      }
      .dis_bottom {
        width: 297px;
        height: 28px;
        padding: 0 10px;
        margin: 0 15px 5px 43px;
        background-image: linear-gradient(-225deg, rgba(252, 134, 95, 0.05), rgba(252, 98, 95, 0.05) 100%);
        font-size: 12px;
        line-height: 28px;
        span {
          float: left;
        }
        .van-icon {
          float: right;
          margin-top: 8px;
        }
      }
    }
    .goods {
      width: 100%;
      .good_item {
        margin-top: 10px;
        width: 100%;
        height: 3.29333rem;
        background-color: #fff;
        .checkbox {
          width: 22px;
          height: 20px;
          background-image: url(./images/checkbox.webp);
          background-position: 154px 45px;
          // background-position: 0 -3.4rem;
          background-size: 22px 324px;
          margin-right: 4px;
        }
        .checked {
          width: 22px;
          height: 30px;
          background-image: url(./images/checkbox.webp);
          // background-position: 154px 45px;
          background-position: 0 -3.4rem;
          background-size: 22px 324px;
          margin-right: 4px;
        }
        // .good_header {
        //   width: 360px;
        //   height: 40px;
        //   box-sizing: border-box;
        //   padding: 10px 15px 10px 0px;
        //   margin-left: 15px;
        //   font-size: 14px;
        //   display: flex;
        //   align-items: center;
        //   .good_header_txt {
        //     display: flex;
        //     align-items: center;
        //     height: 20px;
        //     flex: 1;
        //     span {
        //       font-size: 12px;
        //       background-color: #fc865f;
        //       color: #fff;
        //       padding: 0 4px;
        //       margin-right: 6px;
        //     }
        //     p {
        //       flex: 1;
        //     }
        //     div {
        //       color: #dd1a21;
        //     }
        //   }
        // }

        .good_center {
          width: 332px;
          height: 155px;
          margin-left: 43px;
          font-size: 14px;
          position: relative;
          .good_i {
            position: absolute;
            left: -28px;
            top: 40px;
          }
          .gd {
            width: 317px;
            padding: 12px 15px 0 0;
            height: 110px;
            .gd_img {
              width: 86px;
              height: 86px;
              margin-right: 10px;
              background-color: #eee;
              border-radius: 4px;
              float: left;
              img {
                width: 100%;
                height: 100%;
              }
            }

            .gd_txt {
              float: left;
              width: 220px;
              position: relative;
              .gd_txt_p {
                margin: 0;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              }
              span {
                color: #f48f18;
              }
              .type {
                width: 156px;
                height: 18px;
                background-color: #fafafa;
                color: #7f7f7f;
                border: 1px solid #efefef;
                line-height: 18px;
                font-size: 12px;
                padding: 0 5px;
                margin-top: 5px;
              }
              .price {
                margin-top: 8px;
                font-size: 14px;
                i {
                  color: #999;
                  font-size: 12px;
                  font-style: normal;
                  text-decoration: line-through;
                }
              }
              .gd_count {
                position: absolute;
                display: flex;
                right: 1px;
                bottom: -6px;

                .gd_btn {
                  background-color: #fff;
                  border: 1px solid #eee;
                  width: 29px;
                  height: 26px;
                }
                .gd_input {
                  width: 29px;
                  height: 26px;
                  padding: 0;
                  border: 1px solid #bbb;
                  padding-left: 10px;
                  box-sizing: border-box;
                  line-height: 26px;
                }
              }
            }
            .gt_bottom {
              float: left;
              margin-top: 2px;
              font-size: 12px;
              color: #7f7f7f;
              height: 20px;
            }
          }
        }
      }
    }
  }
  .checkstand {
    position: fixed;
    bottom: 45px;
    width: 100%;
    height: 59px;
    background-color: #fff;
    border: 1px solid #ddd;
    border-left: none;
    border-right: none;
    display: flex;
    align-items: center;
    font-size: 14px;
    .checkbox {
      width: 20px;
      height: 20px;
      background-image: url(./images/checkbox.webp);
      background-position: 154px 45px;
      background-size: 22px 324px;
      margin-right: 4px;
      margin-left: 15px;
    }
    .checked {
      width: 22px;
      height: 30px;
      background-image: url(./images/checkbox.webp);
      // background-position: 154px 45px;
      background-position: 0 -3.4rem;
      background-size: 22px 324px;
      margin-right: 4px;
    }
    .selected {
      flex: 1;
    }
    .total {
      color: #dd1a21;
    }
    .make_order {
      width: 113px;
      height: 59px;
      background-color: #ccc;
      text-align: center;
      line-height: 59px;
      color: white;
      margin-left: 4px;
    }
    .ok {
      width: 113px;
      height: 59px;
      background-color: red;
      text-align: center;
      line-height: 59px;
      color: white;
      margin-left: 4px;
    }
  }
}
</style>
